package com.gaswell.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.gaswell.pojo.User;
import com.gaswell.service.LoginService;
import com.gaswell.service.RoleandpermService;
import com.gaswell.service.UserService;
import com.gaswell.utils.DESUtils;
import com.gaswell.utils.JwtUtils;
import com.gaswell.vo.LoginUser;
import com.gaswell.vo.Result;
import com.gaswell.vo.params.LoginParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
@DS("mysql")
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AuthenticationManager authenticationManager;


    private static final String slat="gaswell";
    @Override
    public Result login(LoginParams loginParams) {
        String passWord=loginParams.getPassword();
        String userName=loginParams.getUserName();
        if(StringUtils.isBlank(passWord) || StringUtils.isBlank(userName)){
            return Result.fail(111,"用户名和密码不能为空");
        }
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userName, passWord);
        Authentication authenticate = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
        if (Objects.isNull(authenticate)) {
            throw new RuntimeException("登录失败");
        }
        LoginUser principal = (LoginUser) authenticate.getPrincipal();
        User user1 = principal.getUser();
        Long id = new Long(user1.getUser_id());
        String jwt = JwtUtils.generateAdminJsonWebToken(id.toString(),user1.getUser_department_id().toString());
        System.out.println(jwt);

//        向redis中存储信息
        redisTemplate.opsForValue().set("login" + id, user1);
        return Result.success(jwt);
    }


    /*检验token是否合法，以及redis中是否存储此token信息 ，并根据token返回user*/
    @Override
    public User checkToken(String token) {
        if(StringUtils.isBlank(token)){
            return null;
        }
        Map<String, Object> stringObjectMap = JwtUtils.checkJWT(token);
        if(stringObjectMap==null){
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("login" + token);
        if (Objects.isNull(user)) {
            throw new RuntimeException("token异常");
        }
        return user;
    }
    @Override
    public Result logout(String token) {
        redisTemplate.delete("TOKEN_"+token);
        return Result.success(null);
    }
}
